package com.iplant.wms.serviceImpl.dao.batch;

import com.iplant.base.po.OutResult;
import com.iplant.base.po.Pagination;
import com.iplant.base.po.enums.ExceptionTypes;
import com.iplant.base.utils.annotation.dao.DBBaseDAO;
import com.iplant.base.utils.general.StringUtils;
import com.iplant.base.utils.sql.BaseDAO;
import com.iplant.wms.service.basicenum.mss.MSSBatchPropControlType;
import com.iplant.wms.service.basicenum.mss.MSSBatchPropDataType;
import com.iplant.basic.service.po.bms.BMSEmployee;
import com.iplant.wms.service.po.batch.MSSBatchProp;
import com.iplant.wms.service.po.batch.MSSBatchRule;
import lombok.extern.slf4j.Slf4j;

import java.util.*;

@DBBaseDAO(MSSBatchProp.class)
@Slf4j
public class MSSBatchPropDAO extends BaseDAO {

    private static MSSBatchPropDAO Instance = null;

    private static final int PropTotalMax = 12;

    private MSSBatchPropDAO() {

        super("");this.Init();
    }

    public static MSSBatchPropDAO getInstance() {
        if (Instance == null) {
            Instance = new MSSBatchPropDAO();
        }
        return Instance;
    }


    protected void MSS_CreateBatchProp(BMSEmployee wLoginUser, int wRuleID, OutResult<Integer> wErrorCode) {

        List<Map<String, Object>> wParwmsList = new ArrayList<>();
        Map<String, Object> wParwms;
        for (int i = 1; i <= PropTotalMax; i++) {
            wParwms = new HashMap<>();

            wParwms.put("Name", "批次属性" + String.format("%02d", i));
            wParwms.put("Code", "LotAttr" + String.format("%02d", i));
            wParwms.put("RuleID", wRuleID);
            wParwms.put("VisibleRF", 1);
            wParwms.put("DataType", MSSBatchPropDataType.String.getValue());
            wParwms.put("DataCheck", 0);

            wParwms.put("EditorID", wLoginUser.getID());
            wParwms.put("EditTime", Calendar.getInstance());
            wParwms.put("CreatorID", wLoginUser.getID());
            wParwms.put("CreateTime", Calendar.getInstance());
            wParwms.put("Active", MSSBatchPropControlType.Disable.getValue());

            switch (i) {
                case 1:
                    wParwms.put("Name", "生产日期");
                    wParwms.put("Code", "ProductDate");
                    wParwms.put("Active", MSSBatchPropControlType.Required.getValue());
                    wParwms.put("DataType", MSSBatchPropDataType.Date.getValue());
                    break;
                case 2:
                    wParwms.put("Name", "失效日期");
                    wParwms.put("Code", "ExpireDate");
                    wParwms.put("Active", MSSBatchPropControlType.Required.getValue());
                    wParwms.put("DataType", MSSBatchPropDataType.Date.getValue());
                    break;
                case 3:
                    wParwms.put("Name", "入库日期");
                    wParwms.put("Code", "StockDate");
                    wParwms.put("Active", MSSBatchPropControlType.Required.getValue());
                    wParwms.put("DataType", MSSBatchPropDataType.Date.getValue());
                    break;
                default:
                    break;
            }

            wParwmsList.add(wParwms);

        }

        this.Insert(wParwmsList);

    }

    public List<MSSBatchProp> MSS_QueryBatchPropList(BMSEmployee wLoginUser, int wRuleID, String wRuleCode,
                                                     OutResult<Integer> wErrorCode) {

        List<MSSBatchProp> wMSSBatchPropList = null;
        try {
            if (wRuleID <= 0 || StringUtils.isEmpty(wRuleCode)) {
                wErrorCode.Result = ExceptionTypes.Parameter.getValue();
                return new ArrayList<>();
            }

            Map<String, Object> wParams = new HashMap<>();
            wParams.put("RuleID", wRuleID);
            wParams.put("RuleCode", wRuleCode);

            wMSSBatchPropList =
                    this.SelectPOList(wLoginUser, wParams, Pagination.getNewMaxSize(),
                            wErrorCode);

            if (wErrorCode.Result != 0) {
                return wMSSBatchPropList;
            }

            if (wMSSBatchPropList != null && wMSSBatchPropList.size() != PropTotalMax) {

                if (wRuleID <= 0) {
                    MSSBatchRule mssBatchRule =
                            MSSBatchRuleDAO.getInstance().SelectPO(wLoginUser, -1, wRuleCode, wErrorCode);
                    if (wErrorCode.Result != 0) {
                        return wMSSBatchPropList;
                    }
                    if (mssBatchRule == null || mssBatchRule.ID <= 0) {
                        return wMSSBatchPropList;
                    }
                    wRuleID = mssBatchRule.ID;
                }

                Map<String, Object> wDeleteParwms = new HashMap<>();
                wDeleteParwms.put("RuleID", wRuleID);
                this.Delete(wDeleteParwms, " and ID>0 ");

                this.MSS_CreateBatchProp(wLoginUser, wRuleID, wErrorCode);
                if (wErrorCode.Result != 0) {
                    return wMSSBatchPropList;
                }

                wMSSBatchPropList =
                        this.SelectPOList(wLoginUser, wParams, Pagination.getNewMaxSize(),
                                wErrorCode);
            }


        } catch (Exception ex) {

            wErrorCode.set(ExceptionTypes.DBSQL.getValue());
            log.error("MSS_QueryBatchPropList", ex);
        }
        return wMSSBatchPropList;
    }




    public void MSS_DeleteBatchProp(BMSEmployee wLoginUser, int wRuleID, OutResult<Integer> wErrorCode) {
        wErrorCode.set(0);

        try {
            if (wRuleID <= 0) {
                wErrorCode.set(ExceptionTypes.Parameter.getValue());
                return;
            }
            Map<String, Object> wDeleteParams = new HashMap<>();
            wDeleteParams.put("RuleID", wRuleID);
            this.Delete(wDeleteParams, " and ID>0 ");

        } catch (Exception ex) {
            wErrorCode.set(ExceptionTypes.DBSQL.getValue());
            log.error("MSS_DeleteBatchProp", ex);
        }
    }


    public MSSBatchProp MSS_QueryBatchProp(BMSEmployee wLoginUser, int wRuleID, String wRuleCode, String wCode,
                                           OutResult<Integer> wErrorCode) {
        MSSBatchProp wResult = new MSSBatchProp();

        try {
            if (StringUtils.isEmpty(wCode) || (wRuleID <= 0 && StringUtils.isEmpty(wCode))) {
                wErrorCode.Result = ExceptionTypes.Parameter.getValue();
                return wResult;
            }
            Map<String, Object> wParams = new HashMap<>();
            wParams.put("RuleID", wRuleID);
            wParams.put("RuleCode", wRuleCode);
            wParams.put("Code", wCode);
            List<MSSBatchProp> wMSSBatchPropList =
                    this.SelectPOList(wLoginUser, wParams, Pagination.getNewMaxSize(),
                            wErrorCode);


            if (wErrorCode.Result != 0) {
                return wResult;
            }

            if (wMSSBatchPropList != null && wMSSBatchPropList.size() > 0) {
                wResult = wMSSBatchPropList.get(0);
            }


        } catch (Exception ex) {

            wErrorCode.set(ExceptionTypes.DBSQL.getValue());
            log.error("MSS_QueryBatchProp", ex);
        }
        return wResult;
    }



}
